### texreg method for pooled krls objects
extract.krls.pooled <- function(model.file, include.r2 = TRUE,
                         include.nobs = TRUE, ...) {

  foo <- readRDS(model.file)
  r2 <- median(unlist(lapply(foo, function(x){x$R2})))
  
  coef <- apply(do.call("rbind", lapply(foo, function(x){x$avgderivatives})),2,mean)
  se <- apply(do.call("rbind", lapply(foo, function(x){x$avgderivatives})),2,sd)
  pval <- 2*(1-pnorm(abs(coef/se)))
  
  names <- names(coef)
  
  n <- dim(foo[[1]]$derivatives)[1]

  gof <- numeric()
  gof.names <- character()
  gof.decimal <- logical()
  
  if (include.r2 == TRUE) {
    gof <- c(gof, r2)
    gof.names <- c(gof.names, "R2")
    gof.decimal <- c(gof.decimal, TRUE)
  }

  if (include.nobs == TRUE) {
    gof <- c(gof, n)
    gof.names <- c(gof.names, "Number of Observations")
    gof.decimal <- c(gof.decimal, FALSE)
  }
  
  tr <- createTexreg(
    coef.names = names,
    coef = coef,
    se = se,
    pvalues = pval,
    gof.names = gof.names,
    gof = gof,
    gof.decimal = gof.decimal
  )
  rm(foo)
  return(tr)
}
